package thread;

import org.junit.Test;
import util.ThreadUtil;

import java.util.concurrent.CountDownLatch;

/**
 * @author wuguojian
 * @description
 * @date 2022/7/27 下午3:10
 **/
public class CountDownLatchTest {

    private CountDownLatch cdl = new CountDownLatch(2);

    private Thread masterThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                ThreadUtil.print("开始");
                cdl.await();
                ThreadUtil.print("结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ThreadUtil.print("都执行完了！");
        }
    });

    private Thread workThread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            ThreadUtil.sleep(1000L);
            cdl.countDown();
            ThreadUtil.print("work1");
        }
    });

    private Thread workThread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            ThreadUtil.sleep(2000L);
            cdl.countDown();
            ThreadUtil.print("work2");
        }
    });

    @Test
    public void test(){
        masterThread.start();
        workThread1.start();
        workThread2.start();
        ThreadUtil.sleep(10000);
    }

}
